package neptune.web.server.services;

import javax.servlet.http.HttpSession;
import neptune.manager.model.ApplicationProperties;
import neptune.web.client.model.SerializableApplicationProperties;
import neptune.web.client.services.SecurityException;
import neptune.web.client.services.SystemService;
import neptune.web.server.Debug;
import neptune.web.server.operations.GetApplicationPreferences;
import neptune.web.server.operations.UpdateApplicationPreferences;
import neptune.web.server.security.SecurityHelper;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SystemServiceImpl extends RemoteServiceServlet implements SystemService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see neptune.web.client.services.SystemService#getSystemConfiguration()
	 */
	public SerializableApplicationProperties getSystemConfiguration() throws SecurityException, Exception
	{
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			GetApplicationPreferences operation = (GetApplicationPreferences) SecurityHelper.getOperation(GetApplicationPreferences.ID, session);
			ApplicationProperties appProp = (ApplicationProperties) operation.execute(null)[0];
			SerializableApplicationProperties serAppProp = SerializationHelper.toSerializableApplicationProperties(appProp); 
			return serAppProp;
		} 
		catch (SecurityException e) 
		{
			throw new SecurityException(e.getMessage());
		}
		catch (Exception e) 
		{
			Debug.printTrace(e);
			throw new Exception(e);
		}
	}

	/* (non-Javadoc)
	 * @see neptune.web.client.services.SystemService#updateSystemConfiguration(neptune.web.client.model.SerializableApplicationProperties)
	 */
	public Boolean updateSystemConfiguration(
			SerializableApplicationProperties serAppProperties)
			throws SecurityException, Exception 
	{
		try 
		{
			HttpSession session = getThreadLocalRequest().getSession(false);
			UpdateApplicationPreferences operation = (UpdateApplicationPreferences) SecurityHelper.getOperation(UpdateApplicationPreferences.ID, session);
			ApplicationProperties appProp = SerializationHelper.toApplicationProperties(serAppProperties);
			operation.execute(new Object[] { appProp} );
			return new Boolean(true);
		} 
		catch (SecurityException e) 
		{
			throw new SecurityException(e.getMessage());
		}
		catch (Exception e) 
		{
			Debug.printTrace(e);
			throw new Exception(e);
		}
	}

}
